var amapFile = require('../../libs/amap-wx.130.js');

Page({
  data: {
    markers: [],
    distance: '',
    cost: '',
    transits: [],
    polyline: []
  },
  onLoad: function() {
    var that = this;
    let origin=getApp().globalData.originb+','+getApp().globalData.origina
    let des=getApp().globalData.desb+','+getApp().globalData.desa  
    var myAmapFun = new amapFile.AMapWX({key: '038c54183637f97bf87484c0ce1c7b01'});
    myAmapFun.getTransitRoute({
      origin: origin,
      destination: des,
      city: '北京',
      success: function(data){
        console.log('look',data)
        if(data && data.transits){
          var transits = data.transits;
          for(var i = 0; i < transits.length; i++){
            var segments = transits[i].segments;
            transits[i].transport = [];
            for(var j = 0; j < segments.length; j++){
              if(segments[j].bus && segments[j].bus.buslines && segments[j].bus.buslines[0] && segments[j].bus.buslines[0].name){
                var name = segments[j].bus.buslines[0].name
                if(j!==0){
                  name = '--' + name;
                }
                transits[i].transport.push(name);
              }
            }
          }
        }
        that.setData({
          transits: transits
        });
          
      },
      fail: function(info){

      }
    })
  },
  goToCar: function (e) {
    wx.redirectTo({
      url: '../321/321'
    })
  },
  goToBus: function (e) {
    if(getApp().globalData.descity==getApp().globalData.origincity){
      wx.redirectTo({
        url: '../bus/bus?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title:'仅支持同城公交路线规划'
      })
    }
  },
  goToRide: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)
    if(distance<=500000){
      wx.redirectTo({
        url: '../ride/ride'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议骑行'
      })
    }
  },
  goToWalk: function (e) {
    let distance=this.data.distance.substring(0,this.data.distance.length-1)
    if(distance<=100000){
      wx.redirectTo({
        url: '../walk/walk?'
      })
    }
    else{
      wx.showModal({
        cancelColor: 'cancelColor',
        title: '距离较远不建议步行'
      })
    }
  }
})